import NProgress from 'nprogress' // 网页顶部进度
import 'nprogress/nprogress.css' // 进度条样式
NProgress.configure({ showSpinner: false }) // 隐藏旋转图标

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import store from '@/store'
import constantRoutes from './constantRoutes'

const router = new Router({
  // mode: 'history', // require service support (另外支持hash)
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const whiteList = ['/login']// no redirect whitelist // '/authredirect' 微信登陆

router.beforeEach((to, from, next) => {
  NProgress.start() // 进度条开始
  // 白名单直接进入
  if (whiteList.indexOf(to.path) !== -1) {
    return next()
  }
  // 已经登陆
  if (store.state.user.token) {
    if (to.path === '/login') {
      return next({ path: '/' })
    }
    return next()
  }
  // 没有登陆重定向登陆页面
  next('/login')
  NProgress.done() // 进度条结束
})

router.afterEach(() => {
  NProgress.done() // 进度条结束
})

export default router
